CREATE OR REPLACE FUNCTION base64_encode(bytea) RETURNS text AS '$libdir/pgds', 'base64_encode' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION base64_decode(text) RETURNS bytea AS '$libdir/pgds', 'base64_decode' LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION gen_keys_pair(algorithm text, num_bits int) RETURNS text AS '$libdir/pgds', 'gen_keys_pair' LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION gen_keys_pair(num_bits int) RETURNS text AS $$
	select gen_keys_pair('rsa', $1)
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION get_public_key(private_key text) RETURNS text AS '$libdir/pgds', 'get_public_key' LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION rsa_private_encrypt(private_key text, data bytea) RETURNS bytea AS '$libdir/pgds', 'rsa_private_encrypt' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION rsa_public_decrypt(public_key text, data bytea) RETURNS bytea AS '$libdir/pgds', 'rsa_public_decrypt' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION rsa_private_decrypt(private_key text, data bytea) RETURNS bytea AS '$libdir/pgds', 'rsa_private_decrypt' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION rsa_public_encrypt(public_key text, data bytea) RETURNS bytea AS '$libdir/pgds', 'rsa_public_encrypt' LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION sign(algorithm text, private_key text, data bytea) RETURNS bytea AS '$libdir/pgds', 'sign' LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION sign(private_key text, data bytea) RETURNS bytea AS $$
	select sign('rsa', $1, $2)
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION verify(algorithm text, public_key text, data bytea, signature bytea) RETURNS int AS '$libdir/pgds', 'verify' LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION verify(public_key text, data bytea, signature bytea) RETURNS int AS $$
	select verify('rsa', $1, $2, $3)
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION encrypt(algorithm text, pw text, md_algorithm text, rounds int, salt bytea, iv bytea, data bytea) RETURNS bytea AS '$libdir/pgds', 'encrypt' LANGUAGE C STRICT;
CREATE OR REPLACE FUNCTION decrypt(algorithm text, pw text, md_algorithm text, rounds int, salt bytea, iv bytea, data bytea) RETURNS bytea AS '$libdir/pgds', 'decrypt' LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION encrypt(algorithm text, pw text, md_algorithm text, iv bytea, data bytea) RETURNS bytea AS $$
	select encrypt($1, $2, $3, 1, '', $4, $5)
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION encrypt(algorithm text, pw text, md_algorithm text, data bytea) RETURNS bytea AS $$
	select encrypt($1, $2, $3, 1, '', '', $4)
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION encrypt(algorithm text, pw text, data bytea) RETURNS bytea AS $$
	select encrypt($1, $2, 'sha1', 1, '', '', $3)
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION decrypt(algorithm text, pw text, md_algorithm text, iv bytea, data bytea) RETURNS bytea AS $$
	select encrypt($1, $2, $3, 1, '', $4, $5)
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION decrypt(algorithm text, pw text, md_algorithm text, data bytea) RETURNS bytea AS $$
	select encrypt($1, $2, $3, 1, '', '', $4)
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION decrypt(algorithm text, pw text, data bytea) RETURNS bytea AS $$
	select encrypt($1, $2, 'sha1', 1, '', '', $3)
$$ LANGUAGE SQL;

